/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.other;

import javax.swing.JTextPane;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.StyleConstants;

/**
 *
 * @author Mido
 */
public class JTextPaneProperties {

    private JTextPane text_pane;    // the text pane object the
    private int font_size;  // size of the font
    private String selected_font;   // the selected font from the list of fonts

    /**
     * Constructor with parameters
     *
     * @param text_pane
     * @param font_size size of the font
     * @param selected_font the selected font
     * @param list_font_styles a list containing all the fonts
     */
    public JTextPaneProperties(JTextPane text_pane, int font_size, String selected_font) {
        this.text_pane = text_pane;
        this.font_size = font_size;
        this.selected_font = selected_font;
    }

    /**
     * Applies the attributes on the text pane
     *
     * @param style
     */
    public void setAttributes(String style) {
        if (style.equals("Underlined") == true) {
            setUnderlined();
        } else if (style.equals("Bold") == true) {
            setBold();
        } else if (style.equals("Italic") == true) {
            setItalic();
        } else if (style.equals("Strikethrough") == true) {
            setStrikethrough();
        } else if (style.equals("Bold Italic") == true) {
            setBoldItalic();
        } else if (style.equals("Normal") == true) {
            setNormal();
        }
    }

    /**
     * Applies the underlined attribute on the text pane
     */
    private void setUnderlined() {
        // enable underlined
        MutableAttributeSet attrs = text_pane.getInputAttributes();
        // enable underlined attribute
        StyleConstants.setUnderline(attrs, true);
        // disable bold attribute
        StyleConstants.setBold(attrs, false);
        // disable italic attribute
        StyleConstants.setItalic(attrs, false);
        // disable sttrikethrough attribute
        StyleConstants.setStrikeThrough(attrs, false);

        // set font
        StyleConstants.setFontFamily(attrs, selected_font);
        // set font size
        StyleConstants.setFontSize(attrs, font_size);

        // set the attributes on the text pane
        text_pane.setParagraphAttributes(attrs, false);
    }

    /**
     * Applies the bold attribute on the text pane
     */
    private void setBold() {
        // enable underlined
        MutableAttributeSet attrs = text_pane.getInputAttributes();
        // enable bold attribute
        StyleConstants.setBold(attrs, true);
        // disable underlined attribute
        StyleConstants.setUnderline(attrs, false);
        // disable italic attribute
        StyleConstants.setItalic(attrs, false);
        // disable sttrikethrough attribute
        StyleConstants.setStrikeThrough(attrs, false);

        // set font
        StyleConstants.setFontFamily(attrs, selected_font);
        // set font size
        StyleConstants.setFontSize(attrs, font_size);

        // set the attributes on the text pane
        text_pane.setParagraphAttributes(attrs, false);
    }

    /**
     * Applies the bold italic attribute on the text pane
     */
    private void setBoldItalic() {
        // enable underlined
        MutableAttributeSet attrs = text_pane.getInputAttributes();
        // enable bold attribute
        StyleConstants.setBold(attrs, true);
        // enable italic attribute
        StyleConstants.setItalic(attrs, true);
        // disable underlined attribute
        StyleConstants.setUnderline(attrs, false);
        // disable sttrikethrough attribute
        StyleConstants.setStrikeThrough(attrs, false);

        // set font
        StyleConstants.setFontFamily(attrs, selected_font);
        // set font size
        StyleConstants.setFontSize(attrs, font_size);

        // set the attributes on the text pane
        text_pane.setParagraphAttributes(attrs, false);
    }

    /**
     * Applies the italic attribute on the text pane
     */
    private void setItalic() {
        // enable underlined
        MutableAttributeSet attrs = text_pane.getInputAttributes();
        // enable italic attribute
        StyleConstants.setItalic(attrs, true);
        // disable bold attribute
        StyleConstants.setBold(attrs, false);
        // disable underlined attribute
        StyleConstants.setUnderline(attrs, false);
        // disable sttrikethrough attribute
        StyleConstants.setStrikeThrough(attrs, false);

        // set font
        StyleConstants.setFontFamily(attrs, selected_font);
        // set font size
        StyleConstants.setFontSize(attrs, font_size);

        // set the attributes on the text pane
        text_pane.setParagraphAttributes(attrs, false);
    }

    /**
     * Applies the strikethrough attribute on the text pane
     */
    private void setStrikethrough() {
        // enable underlined
        MutableAttributeSet attrs = text_pane.getInputAttributes();
        // enable sttrikethrough attribute
        StyleConstants.setStrikeThrough(attrs, true);
        // disable bold attribute
        StyleConstants.setBold(attrs, false);
        // disable underlined attribute
        StyleConstants.setUnderline(attrs, false);
        // disable italic attribute
        StyleConstants.setItalic(attrs, false);

        // set font
        StyleConstants.setFontFamily(attrs, selected_font);
        // set font size
        StyleConstants.setFontSize(attrs, font_size);

        // set the attributes on the text pane
        text_pane.setParagraphAttributes(attrs, false);
    }

    /**
     * Applies the normal attribute on the text pane
     */
    private void setNormal() {
        // enable underlined
        MutableAttributeSet attrs = text_pane.getInputAttributes();
        // disable sttrikethrough attribute
        StyleConstants.setStrikeThrough(attrs, false);
        // disable bold attribute
        StyleConstants.setBold(attrs, false);
        // disable underlined attribute
        StyleConstants.setUnderline(attrs, false);
        // disable italic attribute
        StyleConstants.setItalic(attrs, false);

        // set font
        StyleConstants.setFontFamily(attrs, selected_font);
        // set font size
        StyleConstants.setFontSize(attrs, font_size);

        // set the attributes on the text pane
        text_pane.setParagraphAttributes(attrs, false);
    }
}
